package com.mentorgen.tools.profile.runtime;

import com.mentorgen.tools.profile.Controller;
import com.mentorgen.tools.profile.runtime.jar.TempBundleManager;

public class EntityDicHelper {
	
	static final String getMethodKey(String className, String methodName){
		
//		String loaderID = EntityDictionary.getloaderClassDic().get(className);
//		return loaderID + ":" + 
		return className.replace('/', '.') + ":" + methodName;
		
	}
	
	static final String getMethodKey(String loaderID, String bundleName,
			String className, String methodName){
//		return loaderID + ":" + 
		return className.replace('/', '.') + ":" + methodName;
	}
	
	static final String getMethodNameFromKey(String key){
		return key.substring(key.lastIndexOf(":")+1);
	}
	
	static final String getClassNameFromKey(String key){
		return key.substring(0, key.lastIndexOf(":"));
	}
	
	public static final void addMethod2MethodData(String callerClass, String callerName, 
			String calleeClass, String calleeName, long dataSize, boolean isToCallee){

		if (Controller._debug)
			System.err.println("INTERCALL :: ["+callerClass+":"+callerName+"] (called)=> ["+calleeClass+":"+calleeName+"] (" + dataSize +")");
		
		String callerKey = EntityDicHelper.getMethodKey(
				EntityDicHelper.addClassExtension(callerClass), callerName);
		String calleeKey = EntityDicHelper.getMethodKey(
				EntityDicHelper.addClassExtension(calleeClass), calleeName);
		
		try {
			// if data is going to the callee from the caller
			if (isToCallee){
				EntityDictionary.getMethodDic().get(callerKey).dataToMethod(calleeKey, dataSize);
			}
			// if data is going from the callee to the caller
			else {
				EntityDictionary.getMethodDic().get(calleeKey).dataToMethod(callerKey, dataSize);
			}
		}catch (NullPointerException e){
			if (Controller._debug)
				System.err.println("Exception thrown when method to method called");
		}
	}
	
	public static final String dropClassExtension(String className){
		if (className.indexOf(TempBundleManager.CLASS_EXTENSION) == -1)
			return className;
		return className.substring(0, className.indexOf(TempBundleManager.CLASS_EXTENSION));
	}
	
	public static final String addClassExtension(String className){
		if (className.indexOf(TempBundleManager.CLASS_EXTENSION) == -1)
			return className+TempBundleManager.CLASS_EXTENSION;
		return className;
	}
	
	public static final void addMethod2Dic(String loaderID, String bundleName, String className, 
			String superName, String interfaces, String methodName){
		
		String tmpClassName = EntityDicHelper.addClassExtension(className.replaceAll("/", "."));
		
		if (EntityDictionary.getMethodDic().get(EntityDicHelper.getMethodKey(tmpClassName, methodName)) == null){
			EntityDictionary.getMethodDic().put(EntityDicHelper.getMethodKey(tmpClassName, methodName), 
				new Method(loaderID, bundleName, className, superName, interfaces, methodName));
			if (Controller._debug)
				System.err.println("Method " + bundleName + ":" + tmpClassName + ":" + methodName + " added");
		}
	}

}
